package cn.fintecher.pangolin.service.dataimp.task;

import cn.fintecher.pangolin.entity.elastic.DataImportRecord;
import cn.fintecher.pangolin.entity.elastic.ImportTemplate;
import cn.fintecher.pangolin.entity.elastic.ImportTemplateItem;
import cn.fintecher.pangolin.entity.elastic.WorkOrderModel;
import cn.fintecher.pangolin.service.dataimp.elastic.BaseCaseElastic;
import cn.fintecher.pangolin.service.dataimp.model.ImportCommonModel;
import cn.fintecher.pangolin.service.dataimp.model.ImportModel;
import cn.fintecher.pangolin.service.dataimp.repository.CaseInfoRepository;
import cn.fintecher.pangolin.service.dataimp.service.BaseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 14:55 2019/11/5
 */

@Service("importWorkOrderFuture")
public class ImportWorkOrderFuture {
    @Autowired
    private BaseService baseService;

    @Autowired
    private BaseCaseElastic baseCaseElastic;

    @Autowired
    private CaseInfoRepository caseInfoRepository;

    /**
     * 多线程处理工单excel数据
     */
    @Async
    public CompletableFuture<List<ImportModel>> processWorkOrderExcel(List<Map<String, String>> mapList, int sheetIndex, String batchNumber, Map<String,
            ImportTemplateItem> itemMap, Integer num, DataImportRecord record, ImportTemplate importTemplate) {
        int row = importTemplate.getDataStartRow() + num;
        List<ImportModel> models = new ArrayList<>();
        for (Map<String, String> map : mapList) {
            ImportModel model = new ImportModel();
            WorkOrderModel workOrderModel = new WorkOrderModel();
            ImportCommonModel importCommonModel = new ImportCommonModel();
            importCommonModel.setWorkOrderModel(workOrderModel);
            List<String> errors = new ArrayList<>();
            for (Map.Entry<String, ImportTemplateItem> entry : itemMap.entrySet()) {
                baseService.rowToObj(importCommonModel, entry, map, row, errors, sheetIndex);
            }
            //判断必填项
            String caseNumber = workOrderModel.getCaseNumber();
            if (StringUtils.isBlank(caseNumber)) {
                errors.add("第【" + row + "】行中的数据【案件编号】对应值为空");
            } else {
                //检查案件是否存在
                boolean b = baseService.checkCase(importCommonModel.getWorkOrderModel().getCaseNumber(), baseCaseElastic, caseInfoRepository);
                if (!b) {
                    errors.add("第【" + sheetIndex + "】个sheet页中第【" + row + "】行案件编号为【" + importCommonModel.getWorkOrderModel().getCaseNumber() + "】的案件不存在");
                }
            }
            Date date = workOrderModel.getDate();
            if (Objects.isNull(date)) {
                errors.add("第【" + row + "】行中的数据【工单日期】对应值为空");
            }
            String content = workOrderModel.getContent();
            if (StringUtils.isBlank(content)) {
                errors.add("第【" + row + "】行中的数据【工单信息】对应值为空");
            }
            workOrderModel.setId(UUID.randomUUID().toString().replaceAll("-", "")); //主键ID
            workOrderModel.setBatchNumber(batchNumber); //批次号
            workOrderModel.setPrincipalId(record.getPrincipalId()); //委托方ID
            workOrderModel.setPrincipalName(record.getPrincipalName()); //委托方名称
            model.setWorkOrderModel(workOrderModel);
            model.setErrors(errors);
            models.add(model);
            row++;
        }
        return CompletableFuture.completedFuture(models);
    }

}
